<?php

/**
 * ECSHOP 管理中心优惠活动管理
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: favourable.php 17217 2011-01-19 06:29:08Z liubo $
 */
 
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');

/*------------------------------------------------------ */
//-- 活动列表页
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'list')
{
    admin_priv('users_manage');

    /* 模板赋值 */
    $smarty->assign('full_page',   1);
    $smarty->assign('ur_here',     '佣金分发记录');
    $smarty->assign('action_link', array('href' => 'mshop_frozen_profit.php?act=add', 'text' => '添加佣金分发记录'));

    $list = mshop_frozen_profit_list();
// print_r($list);exit;

    $smarty->assign('mshop_frozen_profit_list', $list['item']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    /* 显示商品列表页面 */
    assign_query_info();
    $smarty->display('mshop_frozen_profit_list.htm');
}

/*------------------------------------------------------ */
//-- 分页、排序、查询
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{
    $list = mshop_frozen_profit_list();

    $smarty->assign('mshop_frozen_profit_list', $list['item']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('mshop_frozen_profit_list.htm'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

/*------------------------------------------------------ */
//-- 删除
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('users_manage');

    $id = intval($_GET['id']);
    $mshop_frozen_profit = mshop_frozen_profit_info($id);

    if (empty($mshop_frozen_profit))
    {
        make_json_error($_LANG['favourable_not_exist']);
    }
    $name = $mshop_frozen_profit['user_name'];
    //是否存在提现账户
    $cashbank=mshop_cashbank_info($id);
    if($cashbank)
    {
      $links[] = array('text' => '返回店铺列表', 'href' => 'mshop_frozen_profit.php?act=list&' . list_link_postfix());
      make_json_error($_LANG['drop_error']);
      exit();
    }

       
    $sql=" delete from ".$GLOBALS['ecs']->table('mshop_frozen_profit')." where frozen_id=".$id;
  
    $status=$GLOBALS['db']->query($sql);


    /* 记日志 */
    admin_log($name, 'remove', 'mshop_frozen_profit');

    /* 清除缓存 */
    clear_cache_files();

    $url = 'mshop_frozen_profit.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
    ecs_header("Location: $url\n");
    exit;
}

/*------------------------------------------------------ */
//-- 批量操作
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'batch')
{

    /* 取得要操作的记录编号 */
    if (empty($_POST['checkboxes']))
    {
        sys_msg($_LANG['no_record_selected']);
    }
    else
    {
        /* 检查权限 */
        admin_priv('users_manage');


        $ids = $_POST['checkboxes'];
        if (isset($_POST['drop']))
        {

              /* 删除记录 */
                $sql = "DELETE FROM " . $ecs->table('mshop_frozen_profit') .
                        " WHERE frozen_id " . db_create_in($ids);
                $status=$db->query($sql);              
            




            /* 记日志 */
            admin_log('', 'batch_remove', 'mshop_frozen_profit');

            /* 清除缓存 */
            clear_cache_files();

            $links[] = array('text' => '返回店铺列表', 'href' => 'mshop_frozen_profit.php?act=list&' . list_link_postfix());
            sys_msg($_LANG['batch_drop_ok']);
        }
    }
}


/*------------------------------------------------------ */
//-- 添加、编辑
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add' || $_REQUEST['act'] == 'edit')
{
	
	   //暂时屏蔽添加功能
	     $links = array(
            array('href' => 'mshop_frozen_profit.php?act=list', 'text' => $_LANG['back_favourable_list'])
        );
        sys_msg('暂时屏蔽添加功能', 0, $links);
		
    /* 检查权限 */
    admin_priv('users_manage');

    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'add';
    $smarty->assign('form_action', $is_add ? 'insert' : 'update');


    /* 初始化、取得优惠活动信息 */
    if ($is_add)
    {

        $mshop_frozen_profit = array(
            'frozen_id'        => '',
            'user_name'      => '',
            'frozen_profit'      => 0,
			 'status'      => 0
        );
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }
        $id = intval($_GET['id']);
        $mshop_frozen_profit = mshop_frozen_profit_info($id);
        if (empty($mshop_frozen_profit))
        {
            sys_msg($_LANG['favourable_not_exist']);
        }

    }
    $smarty->assign('mshop_frozen_profit', $mshop_frozen_profit);


    // print_r($mshop_frozen_profit);exit();


    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', $_LANG['add_favourable']);
    }
    else
    {
        $smarty->assign('ur_here', $_LANG['edit_favourable']);
    }
    $href = 'mshop_frozen_profit.php?act=list';
    if (!$is_add)
    {
        $href .= '&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => $_LANG['favourable_list']));
    assign_query_info();
    $smarty->display('mshop_frozen_profit_info.htm');
}

/*------------------------------------------------------ */
//-- 添加、编辑后提交
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'insert' || $_REQUEST['act'] == 'update')
{
    /* 检查权限 */
    admin_priv('users_manage');

    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert';

    /* 检查可提现积分 */
    $pay_name = $_POST['pay_name']? $_POST['pay_name']:"";
    $pay_fee = ($_POST['pay_fee'])?$_POST['pay_fee']: 0;
    $pay_order = intval($_POST['pay_order']) >= 0 ? intval($_POST['pay_order']) : 0;
    $enabled = intval($_POST['enabled'])? intval($_POST['enabled']):0;
    $id = intval($_POST['id'])? intval($_POST['id']) : 0;

    

    /* 提交值 */
    $mshop_frozen_profit = array(
        'frozen_id'      => $id,
        'pay_name'      => $pay_name,
        'pay_fee'      => $pay_fee,
        'pay_order'      => $pay_order,
        'enabled'      => $enabled

    );





    /* 保存数据 */
    if ($is_add)
    {

        $db->autoExecute($ecs->table('mshop_frozen_profit'), $mshop_frozen_profit, 'INSERT');
        $mshop_frozen_profit['mshop_frozen_profit_id'] = $db->insert_id();
    }
    else
    {
        $db->autoExecute($ecs->table('mshop_frozen_profit'), $mshop_frozen_profit, 'UPDATE', "frozen_id = '$id'");
         
    }

    /* 记日志 */
    if ($is_add)
    {
        admin_log('添加佣金分发记录'.$mshop_frozen_profit['frozen_id'], 'add', 'mshop_frozen_profit');
    }
    else
    {
        admin_log('编辑佣金分发记录'.$mshop_frozen_profit['frozen_id'], 'edit', 'mshop_frozen_profit');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'mshop_frozen_profit.php?act=add', 'text' => $_LANG['continue_add_favourable']),
            array('href' => 'mshop_frozen_profit.php?act=list', 'text' => $_LANG['back_favourable_list'])
        );
        sys_msg($_LANG['add_favourable_ok'], 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'mshop_frozen_profit.php?act=list&' . list_link_postfix(), 'text' => $_LANG['back_favourable_list'])
        );
        sys_msg($_LANG['edit_favourable_ok'], 0, $links);
    }
}

/*------------------------------------------------------ */
//-- 搜索商品
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'search')
{
    /* 检查权限 */
    check_authz_json('mshop_frozen_profit');

    include_once(ROOT_PATH . 'includes/cls_json.php');

    $json   = new JSON;
    $filter = $json->decode($_GET['JSON']);
    $filter->keyword = json_str_iconv($filter->keyword);
    if ($filter->act_range == FAR_ALL)
    {
        $arr[0] = array(
            'id'   => 0,
            'name' => $_LANG['js_languages']['all_need_not_search']
        );
    }
    else
    {
       $arr=$mshop_frozen_profit_list;
    }

    if (empty($arr))
    {
        $arr = array(0 => array(
            'id'   => 0,
            'name' => $_LANG['search_result_empty']
        ));
    }

    make_json_result($arr);
}

/*
 * 取得店铺列表
 * @return   array
 */
function mshop_frozen_profit_list()
{
    $result = get_filter();

    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);   
        $filter['order_sn']    = empty($_REQUEST['order_sn']) ? '' : trim($_REQUEST['order_sn']);
        $filter['status']    = empty($_REQUEST['status']) ? '' : trim($_REQUEST['status']);
        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
        {
             $filter['keyword'] = json_str_iconv($filter['keyword']);
             $filter['order_sn'] = json_str_iconv($filter['order_sn']);
             $filter['status'] = json_str_iconv($filter['status']);
        }

        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'm.frozen_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $where = "";
        if (!empty($filter['keyword']))
        {
            $where .= " AND u.user_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%'";
        }
        if (!empty($filter['order_sn']))
        {
            $where .= " AND m.order_sn LIKE '%" . mysql_like_quote($filter['order_sn']) . "%'";
        }
        if (!empty($filter['status']))
        {
            $filter['status']=$filter['status']-1;
            $where .= " AND m.status=" . mysql_like_quote($filter['status']);
        }

        $ts_where=" and u.mshop_id!=0";//没有店铺的会员不用连接
        $sql = "SELECT COUNT(*) FROM ". $GLOBALS['ecs']->table('mshop_frozen_profit').' as m'.
				" left join ". $GLOBALS['ecs']->table('users')." as u on u.mshop_id=m.mshop_id ".
                 " WHERE 1 $where".$ts_where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        

        /* 分页大小 */
        $filter = page_and_size($filter);

        // $filter['page_size']=15;//测试分页效果
        // $filter['page_count']=1;
        // print_r($__REQUEST);exit();


        /* 查询 */
        $sql = "SELECT m.*,u.user_name,u.mobile_phone ".
                "FROM " . $GLOBALS['ecs']->table('mshop_frozen_profit').' as m'.
				" left join ". $GLOBALS['ecs']->table('users')." as u on u.mshop_id=m.mshop_id ".
                " WHERE 1 $where ".$ts_where.
                " ORDER BY $filter[sort_by] $filter[sort_order] ".
                " LIMIT ".$filter['start'] .", $filter[page_size]";

        $filter['keyword'] = stripslashes($filter['keyword']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->query($sql);





    $list = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $row['ctime']=date('Y-m-d H:i:s',$row['ctime']);
        $list[]=$row;
    }

    return array('item' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

   function mshop_frozen_profit_info($id)
   {
        $sql="SELECT m.*,u.user_name,u.mobile_phone  ".
                "FROM " . $GLOBALS['ecs']->table('mshop_frozen_profit').' as m'.
				" left join ". $GLOBALS['ecs']->table('users')." as u on u.mshop_id=m.mshop_id ".
                " where m.frozen_id=".$id;
        $row=$GLOBALS['db']->getRow($sql);

        $row['ctime']=date('Y-m-d H:i:s',$row['ctime']);
        return $row;
   }

 //获取该店铺包括所有父店铺--因为只有四级所以不用递归求值
  function get_parent_mshop($mshop_id)
  {
    $mshop_all=array();
    $sql = " SELECT mshop_pid FROM " . $GLOBALS['ecs']->table('mshop') ." WHERE mshop_id= '$mshop_id'";
    $fir=$GLOBALS['db']->getRow($sql);
    $mshop_all[0]=$mshop_id;
    if($fir['mshop_pid'])
    {
      $mshop_all[1]=$fir['mshop_pid'];
      $sql = " SELECT mshop_pid FROM " . $GLOBALS['ecs']->table('mshop')." WHERE mshop_id= '$fir[mshop_pid]'";
      $sec=$GLOBALS['db']->getRow($sql);
      if($sec['mshop_pid'])
      {
          $mshop_all[2]=$sec['mshop_pid'];
      }
     
    }

     return $mshop_all;
  }


?>